package com.qiyi.player.core.model.impls
{
   import flash.events.EventDispatcher;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.core.model.IMovie;
   import com.qiyi.player.core.model.def.TryWatchEnum;
   import com.qiyi.player.core.player.def.PlayerUseTypeEnum;
   import com.qiyi.player.base.rpc.RemoteObjectEvent;
   import com.qiyi.player.core.model.impls.pub.Settings;
   import com.qiyi.player.core.model.events.MovieEvent;
   import com.qiyi.player.base.pub.EnumItem;
   import com.qiyi.player.core.model.utils.CachePool;
   import com.qiyi.player.core.model.def.DefinitionEnum;
   import com.qiyi.player.core.model.utils.DefinitionUtils;
   import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
   import com.qiyi.player.core.model.remote.*;
   import com.qiyi.player.base.logging.Log;
   
   public class MovieChecker extends EventDispatcher
   {
      
      public function MovieChecker(param1:ICorePlayer) {
         this._log = Log.getLogger("com.qiyi.player.core.model.impls.MovieChecker");
         super();
         this._holder = param1;
         this._currentTvid = "";
         this._currentVid = "";
         this._isSuccess = false;
         this._vInfoRemoteRt = false;
         this._movieVerifyRemoteRt = false;
         this._authenticationRemoteRt = false;
         this._requestVRSRemoteRt = false;
      }
      
      private var _log:ILogger;
      
      private var _holder:ICorePlayer;
      
      private var _movie:IMovie;
      
      private var _currentTvid:String;
      
      private var _currentVid:String;
      
      private var _isSuccess:Boolean;
      
      private var _vInfoRemote:VInfoRemote;
      
      private var _movieVerifyRemote:MovieVerifyRemote;
      
      private var _authenticationRemote:AuthenticationRemote;
      
      private var _requestVRSRemote:RequestVRSRemote;
      
      private var _vInfoRemoteRt:Boolean;
      
      private var _movieVerifyRemoteRt:Boolean;
      
      private var _authenticationRemoteRt:Boolean;
      
      private var _requestVRSRemoteRt:Boolean;
      
      public function getIsSuccess() : Boolean {
         return this._isSuccess;
      }
      
      public function getMovie() : IMovie {
         return this._movie;
      }
      
      public function getCurrentTvid() : String {
         return this._currentTvid;
      }
      
      public function getCurrentVid() : String {
         return this._currentVid;
      }
      
      public function clearMovie() : void {
         this.stopAllRemote();
         if(this._movie)
         {
            this._movie.destroy();
            this._movie = null;
         }
         this._currentTvid = "";
         this._currentVid = "";
         this._isSuccess = false;
         this._vInfoRemoteRt = false;
         this._movieVerifyRemoteRt = false;
         this._authenticationRemoteRt = false;
         this._requestVRSRemoteRt = false;
      }
      
      public function checkout(param1:String, param2:String) : void {
         this.clearMovie();
         this._currentTvid = param1;
         this._currentVid = param2;
         this._holder.runtimeData.isTryWatch = false;
         this._holder.runtimeData.tryWatchType = TryWatchEnum.NONE;
         this._holder.runtimeData.tryWatchTime = 0;
         if(!(this._holder.runtimeData.CDNStatus == -1) || this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.PREVIEW || this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.SHARE_SECTION)
         {
            this._vInfoRemoteRt = true;
         }
         else
         {
            this.startVInfoRemote();
         }
         if(this._holder.runtimeData.movieIsMember)
         {
            this.startMovieVerifyRemote();
         }
         else
         {
            this.startRequestVRSRemote();
         }
      }
      
      private function startVInfoRemote() : void {
         if(this._vInfoRemote)
         {
            this._vInfoRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onVInfoRemoteRt);
            this._vInfoRemote.destroy();
         }
         this._vInfoRemote = new VInfoRemote(this._holder);
         this._vInfoRemote.addEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onVInfoRemoteRt);
         this._vInfoRemote.initialize();
      }
      
      private function stopVInfoRemote() : void {
         if(this._vInfoRemote)
         {
            this._vInfoRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onVInfoRemoteRt);
            this._vInfoRemote.destroy();
            this._vInfoRemote = null;
         }
      }
      
      private function startMovieVerifyRemote() : void {
         if(this._movieVerifyRemote)
         {
            this._movieVerifyRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onMovieVerifyRemoteRt);
            this._movieVerifyRemote.destroy();
         }
         this._movieVerifyRemote = new MovieVerifyRemote(this._holder);
         this._movieVerifyRemote.addEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onMovieVerifyRemoteRt);
         this._movieVerifyRemote.initialize();
      }
      
      private function stopMovieVerifyRemote() : void {
         if(this._movieVerifyRemote)
         {
            this._movieVerifyRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onMovieVerifyRemoteRt);
            this._movieVerifyRemote.destroy();
            this._movieVerifyRemote = null;
         }
      }
      
      private function startAuthenticationRemote() : void {
         if(this._authenticationRemote)
         {
            this._authenticationRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onAuthenticationRemoteRt);
            this._authenticationRemote.destroy();
         }
         this._authenticationRemote = new AuthenticationRemote(0,this._holder);
         this._authenticationRemote.addEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onAuthenticationRemoteRt);
         this._authenticationRemote.initialize();
      }
      
      private function stopAuthenticationRemote() : void {
         if(this._authenticationRemote)
         {
            this._authenticationRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onAuthenticationRemoteRt);
            this._authenticationRemote.destroy();
            this._authenticationRemote = null;
         }
      }
      
      private function startRequestVRSRemote() : void {
         if(this._requestVRSRemote)
         {
            this._requestVRSRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onRequestVRSRemoteRt);
            this._requestVRSRemote.destroy();
         }
         this._requestVRSRemote = new RequestVRSRemote(this._holder);
         this._requestVRSRemote.addEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onRequestVRSRemoteRt);
         this._requestVRSRemote.initialize();
      }
      
      private function stopRequestVRSRemote() : void {
         if(this._requestVRSRemote)
         {
            this._requestVRSRemote.removeEventListener(RemoteObjectEvent.Evt_StatusChanged,this.onRequestVRSRemoteRt);
            this._requestVRSRemote.destroy();
            this._requestVRSRemote = null;
         }
      }
      
      private function stopAllRemote() : void {
         this.stopVInfoRemote();
         this.stopMovieVerifyRemote();
         this.stopAuthenticationRemote();
         this.stopRequestVRSRemote();
      }
      
      private function checkMovieVerifyRemote() : void {
         if((this._movieVerifyRemote.getIsLimited()) && !Settings.instance.boss && this._holder.runtimeData.oversea == 1)
         {
            this.stopAllRemote();
            this._holder.runtimeData.errorCode = 5000;
            this._holder.pingBack.sendError(5000);
            dispatchEvent(new MovieEvent(MovieEvent.Evt_Failed));
         }
         else
         {
            this.startAuthenticationRemote();
         }
      }
      
      private function checkRequestVRSRemote() : void {
         var definitionType:EnumItem = null;
         var vrs:Object = this._requestVRSRemote.getData();
         try
         {
            this._movie = new Movie(vrs as String,this._holder.runtimeData.movieIsMember,this._holder);
            if(this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.MAIN)
            {
               this._movie.startLoadAddedSkipPoints();
            }
            if(!this._holder.runtimeData.movieIsMember)
            {
               CachePool.saveVRSSource(this._currentTvid,this._currentVid,vrs as String);
            }
            definitionType = null;
            if(this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.PREVIEW || this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.SHARE_SECTION)
            {
               definitionType = DefinitionEnum.LIMIT;
            }
            else
            {
               definitionType = DefinitionUtils.getCurrentDefinition(this._holder);
            }
            this._movie.setCurAudioTrack(Settings.instance.audioTrack,definitionType);
            if(this._holder.runtimeData.originalEndTime > this._movie.duration)
            {
               this._holder.runtimeData.originalEndTime = this._movie.duration;
            }
            if((this._movie.ipLimited) && !this._holder.runtimeData.movieIsMember && this._holder.runtimeData.oversea == 1 && !Settings.instance.boss)
            {
               this.stopAllRemote();
               this._holder.runtimeData.errorCode = 5000;
               this._holder.pingBack.sendError(5000);
               dispatchEvent(new MovieEvent(MovieEvent.Evt_Failed));
            }
            else
            {
               this._currentTvid = "";
               this._currentVid = "";
               this._isSuccess = true;
               dispatchEvent(new MovieEvent(MovieEvent.Evt_Success));
            }
         }
         catch(e:Error)
         {
            _log.info("create movie error:" + e.message);
            _holder.pingBack.sendError(103);
            _holder.runtimeData.errorCode = 103;
            dispatchEvent(new MovieEvent(MovieEvent.Evt_Failed));
            stopAllRemote();
            _currentTvid = "";
            _currentVid = "";
         }
      }
      
      private function onVInfoRemoteRt(param1:RemoteObjectEvent) : void {
         this._vInfoRemoteRt = true;
         if(this._holder.runtimeData.movieIsMember)
         {
            if(this._movieVerifyRemoteRt)
            {
               this.checkMovieVerifyRemote();
            }
         }
         else if(this._requestVRSRemoteRt)
         {
            this.checkRequestVRSRemote();
         }
         
      }
      
      private function onMovieVerifyRemoteRt(param1:RemoteObjectEvent) : void {
         this._movieVerifyRemoteRt = true;
         if(this._movieVerifyRemote.status == RemoteObjectStatusEnum.Success)
         {
            if(this._vInfoRemoteRt)
            {
               this.checkMovieVerifyRemote();
            }
         }
         else
         {
            this.stopAllRemote();
            dispatchEvent(new MovieEvent(MovieEvent.Evt_Failed));
            this._currentTvid = "";
            this._currentVid = "";
         }
      }
      
      private function onAuthenticationRemoteRt(param1:RemoteObjectEvent) : void {
         var _loc2_:Object = null;
         this._authenticationRemoteRt = true;
         if(this._authenticationRemote.status == RemoteObjectStatusEnum.Success)
         {
            _loc2_ = this._authenticationRemote.getData();
            if(_loc2_.code == "A00000")
            {
               this.startRequestVRSRemote();
            }
            else
            {
               this._log.info("failed to Authentication. code = " + _loc2_.code);
               this.stopAllRemote();
               this._holder.runtimeData.errorCode = 504;
               dispatchEvent(new MovieEvent(MovieEvent.Evt_Failed));
            }
         }
         else
         {
            this.stopAllRemote();
            dispatchEvent(new MovieEvent(MovieEvent.Evt_Failed));
            this._currentTvid = "";
            this._currentVid = "";
         }
      }
      
      private function onRequestVRSRemoteRt(param1:RemoteObjectEvent) : void {
         this._requestVRSRemoteRt = true;
         if(this._requestVRSRemote.status == RemoteObjectStatusEnum.Success)
         {
            if(this._vInfoRemoteRt)
            {
               this.checkRequestVRSRemote();
            }
         }
         else
         {
            this.stopAllRemote();
            dispatchEvent(new MovieEvent(MovieEvent.Evt_Failed));
            this._currentTvid = "";
            this._currentVid = "";
         }
      }
   }
}
